package com.zgy.myshellcommand.command;


import com.zgy.myshellcommand.anno.CommandConfig;
import com.zgy.myshellcommand.handler.CryptHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.shell.command.CommandRegistration;

import static com.zgy.myshellcommand.enums.FileCoyptOptionsEnum.*;

@CommandConfig
public class CryptCommand {

    @Autowired
    private CryptHandler cryptHandler;

    @Bean
    CommandRegistration encryptHelp(){
        return CommandRegistration.builder()
                .command(Command.getLongName())
                .description(Command.getDescription())
                .group(Command.getGroup())
                .withTarget()
                .consumer((ctx)->cryptHandler.cryptHelp())
                .and()
                .withHelpOptions()
                .longNames("help")
                .shortNames('h')
                .command(Command.getLongName())
                .and()
                .build();

    }


    @Bean
    CommandRegistration encrypt(){
        return CommandRegistration.builder()
                .command(Command.getLongName(), Encrypt.getSubCommandName())
                .description(Encrypt.getDescription())
                .group(Encrypt.getGroup())
                .withOption()
                .description(Str.getDescription())
                .longNames(Str.getLongName())
                .shortNames(Str.getShortName())
                .arity(CommandRegistration.OptionArity.EXACTLY_ONE)
                .and()
                .withOption()
                .description(File.getDescription())
                .longNames(File.getLongName())
                .shortNames(File.getShortName())
                .arity(CommandRegistration.OptionArity.EXACTLY_ONE)
                .and()
                .withOption()
                .description(Passwd.getDescription())
                .longNames(Passwd.getLongName())
                .shortNames(Passwd.getShortName())
                .arity(CommandRegistration.OptionArity.ZERO_OR_ONE)
                .defaultValue(Passwd.getDefaultValue())
                .and()
                .withTarget()
                .function(ctx->cryptHandler.encrypt(ctx))
                .and()
                .withHelpOptions()
                .longNames("help")
                .shortNames('h')
                .command("help")
                .and()
                .hidden()
                .build();
    }

    @Bean
    CommandRegistration decrypt(){
        return CommandRegistration.builder()
                .command(Command.getLongName(), Decrypt.getSubCommandName())
                .description(Decrypt.getDescription())
                .group(Decrypt.getGroup())
                .withOption()
                .description(Str.getDescription())
                .longNames(Str.getLongName())
                .shortNames(Str.getShortName())
                .arity(CommandRegistration.OptionArity.EXACTLY_ONE)
                .and()
                .withOption()
                .description(File.getDescription())
                .longNames(File.getLongName())
                .shortNames(File.getShortName())
                .arity(CommandRegistration.OptionArity.EXACTLY_ONE)
                .and()
                .withOption()
                .description(Passwd.getDescription())
                .longNames(Passwd.getLongName())
                .shortNames(Passwd.getShortName())
                .arity(CommandRegistration.OptionArity.ZERO_OR_ONE)
                .defaultValue(Passwd.getDefaultValue())
                .and()
                .withTarget()
                .function(ctx->cryptHandler.decrypt(ctx))
                .and()
                .withHelpOptions()
                .longNames("help")
                .shortNames('h')
                .command("help")
                .and()
                .hidden()
                .build();
    }

    @Bean
    CommandRegistration readCrypt(){
        return CommandRegistration.builder()
                .command(Command.getLongName(), ReadEncrypt.getSubCommandName())
                .description(ReadEncrypt.getDescription())
                .group(ReadEncrypt.getGroup())
                .withOption()
                .description(File.getDescription())
                .longNames(File.getLongName())
                .shortNames(File.getShortName())
                .arity(CommandRegistration.OptionArity.EXACTLY_ONE)
                .and()
                .withOption()
                .description(Passwd.getDescription())
                .longNames(Passwd.getLongName())
                .shortNames(Passwd.getShortName())
                .arity(CommandRegistration.OptionArity.ZERO_OR_ONE)
                .defaultValue(Passwd.getDefaultValue())
                .and()
                .withTarget()
                .function(ctx -> cryptHandler.readPasswd(ctx))
                .and()
                .withHelpOptions()
                .longNames("help")
                .shortNames('h')
                .command("help")
                .and()
                .hidden()
                .build();

    }

}
